package com.yyzw.activiti.modeler.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyzw.activiti.modeler.dao.FlowMapper;
import com.yyzw.activiti.modeler.model.*;
import com.yyzw.activiti.modeler.service.MyTaskService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MyTaskServiceImpl implements MyTaskService {

    @Autowired
    TaskService taskService;

    @Autowired
    FlowMapper flowMapper;

    @Autowired
    HistoryService historyService;
    @Override
    public DataGridRespVo getUserTaskList(SysUser sysUser, int page, int rows) {
        //查询角色信息
        List<Task> assigneeList = taskService.createTaskQuery().taskAssignee(String.valueOf(sysUser.getUserId())).list();
        List<Task> taskList = taskService.createTaskQuery().taskCandidateUser(String.valueOf(sysUser.getUserId())).list();
        List<Task> candidateGroup = taskService.createTaskQuery().taskCandidateGroup(String.valueOf(sysUser.getRoleId())).list();
        taskList.addAll(assigneeList);
        taskList.addAll(candidateGroup);
        List<MyTaskVo> taskVos =new ArrayList<>();
        MyTaskVo myTaskVo =null;
        for (Task task:taskList) {
            myTaskVo = new MyTaskVo(task);
            SysProjectApply sysProjectApply =(SysProjectApply)taskService.getVariable(task.getId(),"applyData");
            if(sysProjectApply!=null){
                myTaskVo.setUserName(sysProjectApply.getUserName());
                myTaskVo.setApplyName(sysProjectApply.getApplyName());
            }
            taskVos.add(myTaskVo);
        }
        PageHelper.startPage(page,rows);
        PageInfo<MyTaskVo> pageInfo = new PageInfo<>(taskVos);
        DataGridRespVo dataGridRespVo = new DataGridRespVo();
        dataGridRespVo.setRows(taskVos);
        dataGridRespVo.setTotal(pageInfo.getTotal());
        return dataGridRespVo;
    }

    @Override
    public ResponseVo showHanldeTaskById(String taskId) {
        SysProjectApply sysProjectApply =(SysProjectApply)taskService.getVariable(taskId, "applyData");
        List<Task> list = taskService.createTaskQuery().taskId(taskId).list();
        Task task = list.get(0);
        String key = task.getTaskDefinitionKey();
        sysProjectApply.setRemark(key);
        return new ResponseVo(true,"查询成功",sysProjectApply);
    }

    @Override
    public ResponseVo handleTask(TaskHandleVo taskHandleVo) {
        Map<String,Object> variable = taskService.getVariables(taskHandleVo.getTaskId());
        List<TaskHandleVo> handleVos = new ArrayList<>();
        Object optionin = variable.get("handleOptionin");
        if(optionin!=null){
             handleVos =(List<TaskHandleVo>)optionin;
        }
        handleVos.add(taskHandleVo);
        variable.put("handleOptionin",handleVos);
        getVarByUserTask(variable,taskHandleVo);
        taskService.complete(taskHandleVo.getTaskId(),variable);
        return new ResponseVo(true,"审核完成");
    }

    private void getVarByUserTask(Map<String, Object> variable, TaskHandleVo taskHandleVo) {
        Task task = taskService.createTaskQuery().taskId(taskHandleVo.getTaskId()).singleResult();
        switch(task.getTaskDefinitionKey()){
            case "userTask1":{
                if(StringUtils.isNotBlank(taskHandleVo.getType())){
                    variable.put("var1",taskHandleVo.getType());
                }
                break;
            }
            case "userTask2":{
                setVariable(variable,"var2",taskHandleVo.isFlag());
                 break;
            }
            case "userTask3":{
                setVariable(variable,"var3",taskHandleVo.isFlag());
                break;
            }
            case "userTask4":{
                setVariable(variable,"var4",taskHandleVo.isFlag());
                break;
            }
            case "userTask5":{
                setVariable(variable,"var5",taskHandleVo.isFlag());
                break;
            }
            case "userTask6":{

                HistoricProcessInstance instance = historyService.createHistoricProcessInstanceQuery().
                        processInstanceId(task.getProcessInstanceId()).orderByProcessInstanceStartTime().
                        desc().list().get(0);
                if(taskHandleVo.isFlag()){
                    variable.put("var6","0");
                    //更新状态
                    Example example =new Example(SysProjectApply.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andEqualTo("processInstanceId",task.getProcessInstanceId());
                    List<SysProjectApply> select = this.flowMapper.selectByExample(example);
                    SysProjectApply projectApply = select.get(0);
                    projectApply.setStatus("1");
                    this.flowMapper.updateByPrimaryKey(projectApply);
                }else{
                    String key = instance.getProcessDefinitionKey();
                    if(key.equals("userTask4")){
                        variable.put("var6","1");
                    }else{
                        variable.put("var6","2");
                    }
                }
                break;
            }
        }
    }

    private void setVariable(Map<String, Object> variable, String var2,Boolean flag) {
        if(flag){
            variable.put(var2,"1");
        }else{
            variable.put(var2,"0");
        }
    }


}
